local skynet = require "skynet"
local server_def = require "server_def"
local cjson = require "cjson"
local utils = require "utils"
local offline_op = require "offline_op"
local reason = require "reason"
local handler = require "handler"
local constant = require "constant"

local M = {}

local function _pull_payment_info()
    local curr_uid = player.uid
    local ok,ret = skynet.call('.forwarder','lua','TOPAYSVR','query',curr_uid)
    if not ok then
        errlog('failed to query payment info',curr_uid)
        return
    end

    for _,payment_str in ipairs(ret) do
        --判断要购买的商品id
        local payment = cjson.decode(payment_str)
        local ok,ret = pcall(player.add_paid_product,player,payment)
        if not ok then
            errlog(ret)
        end
    end
end

function M.pull_payment_info_for_login()
    local ok,ret = pcall(_pull_payment_info)
    if not ok then
        errlog(ret)
    end
end

function M.pull_payment_info()
    local ok,ret = pcall(_pull_payment_info)
    if not ok then
        errlog(ret)
    end

    handler.notify_user_money(player)
end

local function handle_offline_op(op_data)
    local reason = REASON.PLAY_CARD
    local value = assert(tonumber(op_data.value))
    local key = assert(op_data.key)

    if key == offline_op.OFFLINE_ADD_CONIS then

    end

    return true
end

local function _pull_offline_op_info()
    local curr_uid = player.uid
    local ok,ret = skynet.call('.forwarder','lua','TOEXDBSVR','pull_offline_data',curr_uid)
    if not ok then
        errlog('failed to query offline info',curr_uid)
        return
    end

    for _,data in ipairs(ret) do
        local op_data = cjson.decode(data)
        local ok,ret = pcall(handle_offline_op,op_data)
        if not ok then
            errlog(ret)
        end
    end
end

function M.pull_offline_op_info()
    local ok,ret = pcall(_pull_offline_op_info)
    if not ok then
        errlog(ret)
    end

    handler.notify_user_money(player)
end
----------------------------------------------------matchserver---------------------------------------------
function M.report_player_matched(dest,table_gid,game_type)
    send_to_gateway('hall.NTF_MATCH_SUCESS',{dest = dest,table_gid = table_gid,game_type = game_type})
end
----------------------------------------------------matchserver---------------------------------------------
function M.send_opration_message(result,is_notice)
    for _, v in pairs(result) do
        local begin_time,end_time,type
        --print("==========horse time====",v.is_circul,tostring_r(v.play_start_time),tostring_r(v.play_end_time))
        if is_notice then
            --local time = utils.str_split(v.play_time, '~')
           -- print("time",tostring_r(time))
            begin_time = utils.get_game_time(v.is_circul,v.play_start_time)
            end_time = utils.get_game_time(v.is_circul,v.play_end_time)
        else
            begin_time = v.play_start_time
            end_time = v.play_end_time
        end
        local o = {
            type = v.type or 0,
            message = v.info,
            begin_time = begin_time,
            end_time = end_time,
            interval = v.interval,
            exinfo = v.exinfo
        }
        --print("========send_opration_message===",tostring_r(o))
        send_to_gateway('hall.NTF_CIRCLE_NOTIFICATION',o)
    end
end

function M.new_platform_mail()
    send_to_gateway("mail.NTF_NEW_MAIL",{count = 1})
    send_to_gateway('hall.NTF_RED_POINTS',{module_list = {constant.MODULE_DEF.MAIL} })
end

function M.notify_modify_mail()
    send_to_gateway("mail.NTF_MODIFY_MAIL",{})
end

local function check_update_on_table(uid,amount)
    local ok,table_gid = R().exdbsvr(1):call('.tlock_mgr','get_player_table',uid)
    if not ok then
        errlog(uid,'failed to get_player_table')
        table_gid = 0
    end

    if table_gid ~= 0 then
        local tsvr_id = utils.extract_table_gid(table_gid)
        dest = R().tablesvr(tsvr_id):send('.table_mgr','update_coins_on_table',uid,table_gid,amount)
    end
    return true
end

function M.on_paid(amount)
    local uid = player.uid
    local ok,base_data = R().basesvr({key=uid}):call('.msg_handler','get_base_data',uid)
    if not ok then
        errlog(uid,'failed to get base_data',uid)
        return -1000
    end
    handler.notify_money_changed(player.uid,{
        coins = base_data.coins,
    })
    send_to_gateway("hall.NTF_PAID_SUCC",{amount = amount})
    --通知到游戏
    check_update_on_table(uid,amount)
end

function M.get_enter_data()
    local enter_data = {
        name = player.user_data.name,
        icon = player.user_data.icon,
        sex = player.user_data.sex,
        icon_border = player.user_data.icon_border,
        guns_list = player.user_data.guns_list,
        channel = player.user_data.channel,
        cur_channel = player.user_data.cur_channel,
        client_version = player.user_data.client_version,
        uid = player.uid,
        os_system = player.os_system,
        platform = player.platform,
    }
    skynet.retpack(enter_data)
end

function M.get_player_info()
    local uid = player.uid
    local ok,coins = R().basesvr({key=uid}):call('.msg_handler','get_coins',uid)

    local data = {name = player.user_data.name,coins = coins or 0}
    skynet.retpack(data)
end

function M.notify_money_changed(...)
    handler.notify_money_changed(player.uid,...)
end

function M.send_notify(pbname, notify)
    send_to_gateway(pbname, notify)
end

function M.notify_red_points(point_type)
    send_to_gateway('hall.NTF_RED_POINTS',{
        module_list = {point_type}
    })
end

-----事件通知
local function notify_task_change()
    send_to_gateway('hall.NTF_RED_POINTS',{
        module_list = {constant.MODULE_DEF.TASK}
    })
end

function M.event_on_winning(data)
    local task_type = constant.TASK_TYPE_DEF.TASK_TYPE_WIN_GAMES
    player:update_game_process(task_type,data.game_type,1)

    dbglog("event_on_winning")
    --检查进度是否有更新，有的话则通知变化
    if player:check_task_process(task_type) then
        notify_task_change()
    end
end

function M.event_on_play(data)
    local task_type = constant.TASK_TYPE_DEF.TASK_TYPE_FINISH_GAMES
    player:update_game_process(task_type,data.game_type,1)
    dbglog("event_on_play")
    --检查进度是否有更新，有的话则通知变化
    if player:check_task_process(task_type) then
        notify_task_change()
    end
end

function M.event_on_card_type(data)
    player:update_card_type_process(data.game_type,data.card_type)
    dbglog("event_on_card_type")
    --检查进度是否有更新，有的话则通知变化
    -- if player:check_task_process(task_type) then
    --     notify_task_change()
    -- end
end

function M.send_to_gateway(msgname,msg)
    send_to_gateway(msgname,msg)
end

function M.can_take_bind_phone_award()
    if not player:can_take_bind_phone_award() then --已经领过该奖励了
        skynet.retpack(false) 
        return
    end

    skynet.retpack(true) 
end

function M.on_club_exchange(coins)
    local uid = player.uid
    local exinfo = {channel = player.channel,cur_channel = player.cur_channel}
    local ok,succ,ret = R().basesvr({key=uid}):call('.msg_handler','add_coins',uid,coins,reason.CLUB_EXCHANGE,exinfo)
    if ok and succ and ret then
        handler.notify_money_changed(uid,{coins = ret.curr})
    end
   
    skynet.retpack(true) 
end

function M.takeattach_mail(record)
    local exinfo = player:get_extra_bill_info()
    local ok,succ,ret = R().basesvr({key=player.uid}):call('.msg_handler','take_mail_attach',player.uid,record,exinfo)
    dbglog("ok====succ===",ok,succ)
    if not ok or not succ then
        return skynet.retpack(false)
    end
    handler.notify_money_changed(player.uid, {coins = ret.coins})
    return skynet.retpack(true)
end

return M
